package cloud.keveri.springboot.starter.response4j;

import cloud.keveri.response4j.*;
import cloud.keveri.response4j.aop.DefaultGlobalResponseAdvice;
import cloud.keveri.response4j.configure.Response4jProperties;
import cloud.keveri.response4j.skip.SkipWrapCheckFunction;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * <p>
 * 作者：沈自在
 **/
@Configuration
@EnableConfigurationProperties({Response4jProperties.class})
public class Response4jAutoConfiguration {

    /**
     *
     */
    @Bean
    public ResponseManager responseManager(Response4jProperties prop) {
        return new ResponseManager(prop.getResponseStyle());
    }

    /**
     *
     */
    @Bean
    @ConditionalOnMissingBean(value = ResponseMaker.class)
    public ResponseMaker responseMaker(ResponseManager responseManager, Response4jProperties prop) {
        return new DefaultResponseMaker(responseManager, prop);
    }

    @Bean
    @ConditionalOnMissingBean(value = StringResponseWrapper.class)
    public StringResponseWrapper stringResponseWrapper(ResponseMaker responseMaker) {
        return new DefaultStringResponseWrapper(responseMaker);
    }

    /**
     *
     */
    @Bean
    public DefaultGlobalResponseAdvice defaultGlobalResponseAdvice(ResponseMaker responseMaker,
                                                                   List<SkipWrapCheckFunction> skipWrapCheckFunctions) {
        return new DefaultGlobalResponseAdvice(responseMaker, skipWrapCheckFunctions);
    }
}
